package com.example.forum_system.service.impl;

import com.example.forum_system.dao.ArticleMapper;
import com.example.forum_system.dao.ArticleReplyMapper;
import com.example.forum_system.dao.BoardMapper;
import com.example.forum_system.dao.UserMapper;
import com.example.forum_system.model.Article;
import com.example.forum_system.model.Board;
import com.example.forum_system.model.User;
import com.example.forum_system.model.VO.ArticleVO;
import com.example.forum_system.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: 0.forum_system
 * @BelongsPackage: com.example.forum_system.service.impl
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-12  23:23
 * @Version : 1.0
 * @Description : TODO
 */
@Service
public class ArticleService implements IArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BoardMapper boardMapper;
    @Autowired
    ArticleReplyMapper replyMapper;

    @Override
    public int insertArticle(Article article) {
        initialize(article);
        return articleMapper.insertArticle(article) ;
    }



    @Override
    public List<ArticleVO> selectArticleUser(long userId) {
        List<ArticleVO> articleList = articleMapper.selectArticlesByUser(userId);
        addUser(articleList);
        return articleList;
    }

    @Override
    public int selectArticleCountBoard(long userId) {
        return articleMapper.selectCountByBoard(userId);
    }

    @Override
    public List<ArticleVO> selectArticleBoard(long boardId) {
        List<ArticleVO> articleList = articleMapper.selectArticlesByBoard(boardId);
        addUser(articleList);
        return articleList;
    }

    @Override
    public List<ArticleVO> selectAllArticle() {
        List<ArticleVO> articleList = articleMapper.selectAllArticle();
        addUser(articleList);
        return articleList;
    }

    @Override
    public ArticleVO selectOneArticle(long id) {
        //访问量+1
        articleMapper.addVisitCount(id);
        ArticleVO article = articleMapper.selectOneArticleById(id);
        addUser(article);
        addBoard(article);
        return  article;
    }

    @Override
    public int deleteArticle(long id) {
        return articleMapper.deleteArticle(id);
    }

    @Override
    public int updateArticle(Article article) {
        initialize(article);
        return articleMapper.updateArticle(article);
    }

    @Override
    public int addLike(long id) {
        return articleMapper.addLikeCount(id);
    }

    @Override
    public int addVisit(long id) {
        return articleMapper.addVisitCount(id);
    }

    private void initialize(Article article){
        if(article.getContent() == null){
            article.setContent("#这是一篇文章");
        }
        if(article.getLikeCount() == null){
            article.setLikeCount(0);
        }
        if(article.getVisitCount() == null){
            article.setVisitCount(0);
        }
        if(article.getState() == null){
            article.setState(Byte.valueOf("0"));
        }
        if(article.getDeleteState() == null){
            article.setDeleteState(Byte.valueOf("0"));
        }
        if(article.getCreateTime() == null){
            article.setCreateTime(new Date());
        }
        if(article.getUpdateTime() == null){
            article.setUpdateTime(new Date());
        }
    }

    /*------------------------------------普--通--方--法------------------------------------------------------------------
     * @Desc  : 文章添加用户属性，构成一个重载，可以给列表或单独文章添加用户信息
     * @Param : [articleVOList]
     * @Return: void
    **/
    private void addUser(List<ArticleVO> articleVOList){
        for(ArticleVO article:articleVOList){
            addUser(article);

        }
    }
    private void addUser(ArticleVO article){
        User user = userMapper.selectOneById(article.getUserId());
        article.setUser(user);
        addReplyCount(article);
    }
    private void addBoard(ArticleVO article){
        Board board = boardMapper.selectOneById(article.getBoardId());
        article.setBoard(board);
    }
    private void addReplyCount(ArticleVO article){
        int replyCount = replyMapper.selectCountByArticle(article.getId());
        article.setReplyCount(replyCount);
    }

}
